#include "GunEnemyEntity.h"
#include "EffectManager.h"



void GunEnemyEntity::updateForShoot()
{
	if (_armyData->rect->X - _armyData->targetRect->X > 300) return;
	if (_armyData->bulletContainer->checkCanFire()) {
		_armyData->bulletContainer->fire(_armyData->getRect()->X - 20, _armyData->getRect()->Y + 20, _BULLET_DIRECTION_LEFT, NULL);
		_armyData->sprites->currentSprite->setIndex(1);
	} else {
		_armyData->sprites->currentSprite->setIndex(0);
	}
}

GunEnemyEntity::GunEnemyEntity(int id, int type, double x, double y, Rect* target)
{
	_armyData = new GunEnemyData(id, type, x, y);
	_armyData->targetRect = target;
	life = 5;
}

void GunEnemyEntity::update(long time)
{
	if (_armyData->isDead) return;
	updateForShoot();
	_armyData->bulletContainer->update(time);
	//_data->sprites->updateCurrent();
}

void GunEnemyEntity::draw(long time)
{
	if (_armyData->isDead) return;
	_armyData->sprites->draw();
	_armyData->bulletContainer->draw(time);
}

AbstractData* GunEnemyEntity::getAbstractData()
{
	return _armyData;
}

void GunEnemyEntity::updateData(int action, int actiondata)
{
	if (action == ACTIONS_IS_HURT) {
		life -= actiondata;
		if (life <= 0 && !_armyData->isDead) {
			_armyData->canInteract = false;
			_armyData->isDead = true;
			EffectManager::getInstatnse()->createEffect(EffectType::TYPE_Object_Fire, _armyData->rect->X, _armyData->rect->Y);
		}
	}
}
